package se480.tracker.tracker;

import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

import se480.tracker.TrackerConstants.TrackerEventType;
import se480.tracker.event.TrackerEvent;

/**
 * This Tracker implemenation handles synchronous requests
 * that expect a response
 * 
 * @author mmichalak
 *
 */
public class TrackerB implements Tracker {
	static Logger logger = Logger.getLogger(TrackerB.class.getName());
	/*
	 * NOTE:  the datastore is set in by the Processor
	 * as part of the processor.setTracker(Tracker) method
	 */
	Map<String, ArrayList<Location>> dataStore = null;
	
	@Override
	public void saveCheckpoint() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void savePartialChanges() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String processRequest(TrackerEvent ev) {
		logger.debug("Beginning processRequest for Query");
		
		String response = null;
		String id = ev.getId();
		TrackerEventType query = ev.getEventType();
		
		if (query == TrackerEventType.WHEREIS) {
			response = queryWhereIs(ev);
		}
		else if (query == TrackerEventType.HISTORY) {
			response = queryHistory(ev);
		}
		else if (query == TrackerEventType.LASTUPDATE) {
			response = queryLastUpdate(ev);
		}
		
		return response;
	}
	
	private String queryWhereIs(TrackerEvent ev) {
		String ret = null;
		ArrayList<Location> locs = dataStore.get(ev.getId());
		if (locs == null || locs.size() == 0) {
			ret = "WHEREIS QUERY: id not found - " + ev.getId();
			logger.debug("--------------------> "+ ret);
			return ret;
		}
		
		Location loc = locs.get( locs.size()-1 );
		StringBuilder sb = new StringBuilder(30);
		sb.append(ev.getTick()).append(" QUERY WHEREIS ").append(ev.getId()).append(" ")
			.append(loc.getxCoord()).append(" ").append(loc.getyCoord());
		
		logger.debug("--------------------> WHEREIS QUERY:  " + sb.toString());
		return sb.toString();
	}
	
	private String queryHistory(TrackerEvent ev) {
		String ret = null;
		ArrayList<Location> locs = dataStore.get(ev.getId());
		if (locs == null || locs.size() == 0) {
			ret = "HISTORY QUERY: id not found - " + ev.getId();
			logger.debug("--------------------> " + ret);
			return ret;
		}
		
		StringBuilder sb = new StringBuilder(30);
		sb.append(ev.getTick()).append(" QUERY HISTORY ").append(ev.getId()).append("\n ");
		for (Location loc : locs) {
			sb.append(ev.getTick()).append(" ").append(loc.getxCoord()).append(" ").append(loc.getyCoord()).append("\n ");			
		}
		
		logger.debug("--------------------> HISTORY QUERY:  " + sb.toString());
		return sb.toString();
	}
	
	private String queryLastUpdate(TrackerEvent ev) {
		String ret = null;
		ArrayList<Location> locs = dataStore.get(ev.getId());
		if (locs == null || locs.size() == 0) {
			ret = "WHEREIS LASTUPDATE: id not found - " + ev.getId();
			logger.debug("--------------------> " + ret);
			return ret;
		}
		
		Location loc = locs.get( locs.size()-1 );
		StringBuilder sb = new StringBuilder(30);
		sb.append(ev.getTick()).append(" QUERY LASTUPDATE ").append(ev.getId()).append(" ")
			.append(loc.getTick()).append(" ").append(loc.getxCoord()).append(" ").append(loc.getyCoord());
		
		logger.debug("--------------------> WHEREIS LASTUPDATE:  " + sb.toString());
		return sb.toString();
	}
	
	@Override
	public void setDataStore(Map<String, ArrayList<Location>> dataStore) {
		this.dataStore = dataStore;
	}

	@Override
	public Map<String, ArrayList<Location>> getDataStore() {
		return dataStore;
	}
	

}
